From 0025dbde64f9157f51445db0a5e46eae499efc2f Mon Sep 17 00:00:00 2001 From: Joshua DeSeno Date: Mon, 7 Jul 2014 17:50:05 +0900 Subject: [PATCH] cargo clean. resolves #51 --- Makefile | 1 + src/bin/cargo-clean.rs | 48 ++++++++++++++++++++++++++++++++++++ src/cargo/ops/cargo_clean.rs | 21 ++++++++++++++++ src/cargo/ops/mod.rs | 2 ++ tests/support/mod.rs | 6 ++++- tests/test_cargo_clean.rs | 17 +++++++++++++ tests/tests.rs | 1 + 7 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 src/bin/cargo-clean.rs create mode 100644 src/cargo/ops/cargo_clean.rs create mode 100644 tests/test_cargo_clean.rs diff --git a/Makefile b/Makefile index d1954d2e5..68c48c038 100644 --- a/Makefile +++ b/Makefile @@ -14,6 +14,7 @@ export PATH := $(PATH):$(CURDIR)/rustc/bin # Link flags to pull in dependencies BINS = cargo \ cargo-build \ + cargo-clean \ cargo-read-manifest \ cargo-rustc \ cargo-verify-project \ diff --git a/src/bin/cargo-clean.rs b/src/bin/cargo-clean.rs new file mode 100644 index 000000000..7d9189c3c --- /dev/null +++ b/src/bin/cargo-clean.rs @@ -0,0 +1,48 @@ +#![crate_id="cargo-clean"] +#![feature(phase)] + +extern crate cargo; + +#[phase(plugin, link)] +extern crate hammer; + +#[phase(plugin, link)] +extern crate log; + +extern crate serialize; + +use std::os; +use cargo::ops; +use cargo::{execute_main_without_stdin}; +use cargo::core::MultiShell; +use cargo::util::{CliResult, CliError}; +use cargo::util::important_paths::find_project_manifest; + +#[deriving(PartialEq,Clone,Decodable,Encodable)] +pub struct Options { + manifest_path: Option +} + +hammer_config!(Options) + +fn main() { + execute_main_without_stdin(execute); +} + +fn execute(options: Options, _shell: &mut MultiShell) -> CliResult> { + debug!("executing; cmd=cargo-clean; args={}", os::args()); + + let root = match options.manifest_path { + Some(path) => Path::new(path), + None => try!(find_project_manifest(&os::getcwd(), "Cargo.toml") + .map_err(|_| { + CliError::new("Could not find Cargo.toml in this \ + directory or any parent directory", + 102) + })) + }; + + ops::clean(&root).map(|_| None).map_err(|err| { + CliError::from_boxed(err, 101) + }) +} diff --git a/src/cargo/ops/cargo_clean.rs b/src/cargo/ops/cargo_clean.rs new file mode 100644 index 000000000..2d6788594 --- /dev/null +++ b/src/cargo/ops/cargo_clean.rs @@ -0,0 +1,21 @@ +use std::io::fs::{rmdir_recursive}; +use core::{SourceId}; +use util::{CargoResult, human, ChainError}; +use ops::{read_manifest}; +use std::io::{File}; +use util::toml::{project_layout}; + +pub fn clean(path: &Path) -> CargoResult<()> +{ + let mut file = try!(File::open(path)); + let data = try!(file.read_to_end()); + let layout = project_layout(&path.dir_path()); + let (manifest, _) = try!(read_manifest(data.as_slice(), layout, &SourceId::for_path(path))); + let build_dir = manifest.get_target_dir(); + + if build_dir.exists() { + return rmdir_recursive(build_dir).chain_error(|| human("Could not remove build directory")) + } + + Ok(()) +} diff --git a/src/cargo/ops/mod.rs b/src/cargo/ops/mod.rs index 45ec38101..d47b13620 100644 --- a/src/cargo/ops/mod.rs +++ b/src/cargo/ops/mod.rs @@ -1,7 +1,9 @@ +pub use self::cargo_clean::clean; pub use self::cargo_compile::{compile, CompileOptions}; pub use self::cargo_read_manifest::{read_manifest,read_package,read_packages}; pub use self::cargo_rustc::compile_targets; +mod cargo_clean; mod cargo_compile; mod cargo_read_manifest; mod cargo_rustc; diff --git a/tests/support/mod.rs b/tests/support/mod.rs index 49ab97f93..9dfcd696b 100644 --- a/tests/support/mod.rs +++ b/tests/support/mod.rs @@ -98,7 +98,11 @@ impl ProjectBuilder { } pub fn bin(&self, b: &str) -> Path { - self.root.join("target").join(format!("{}{}", b, os::consts::EXE_SUFFIX)) + self.build_dir().join(format!("{}{}", b, os::consts::EXE_SUFFIX)) + } + + pub fn build_dir(&self) -> Path { + self.root.join("target") } pub fn process(&self, program: T) -> ProcessBuilder { diff --git a/tests/test_cargo_clean.rs b/tests/test_cargo_clean.rs new file mode 100644 index 000000000..4e4c13e5a --- /dev/null +++ b/tests/test_cargo_clean.rs @@ -0,0 +1,17 @@ +use support::{project, execs, main_file, basic_bin_manifest}; +use hamcrest::{assert_that, existing_dir, is_not}; + +fn setup() { +} + +test!(cargo_clean_simple { + let p = project("foo") + .file("Cargo.toml", basic_bin_manifest("foo").as_slice()) + .file("src/foo.rs", main_file(r#""i am foo""#, []).as_slice()); + + assert_that(p.cargo_process("cargo-build"), execs()); + assert_that(&p.build_dir(), existing_dir()); + + assert_that(p.cargo_process("cargo-clean"), execs()); + assert_that(&p.build_dir(), is_not(existing_dir())); +}) diff --git a/tests/tests.rs b/tests/tests.rs index 6dd3bb280..bb5bf9739 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -20,6 +20,7 @@ macro_rules! test( ) ) +mod test_cargo_clean; mod test_cargo_compile; mod test_cargo_compile_git_deps; mod test_cargo_compile_path_deps; -- 2.30.2